#include "timer.h"

extern int time_tick;
extern int sec_tick;

/** Program timer (0,1,2) with mode
 */
void timer_init(int timer, int mode)
{
	outportb(TIMER_CTRL, mode | (timer << 6));
	if (timer == 2)
	{
		unsigned char v = inportb(SPEAKER_CTRL);
	    outportb(SPEAKER_CTRL, v | TIMER2_ENABLE);
	}
}


/** Load timer (0,1,2) with value
 */
void timer_load(int timer, int value)
{
	outportb(TIMER_0 + timer, LSB(value));
	outportb(TIMER_0 + timer, MSB(value));
}

int get_divider(float freq)
{
	if(!freq) return -1;
	return (int)(TIMER_CLK / freq + 0.5);
}
/** Wait mili miliseconds, indirectly counting T0 interrupts
*/
void mili_sleep(int mili)
{

	int tempo = time_tick + mili;
    while (time_tick < tempo); //time_tick varia
}

/** wait secs seconds, indirectly counting T0 interrupts
 */
void sec_sleep(int secs)
{

	mili_sleep(secs*1000);

}
